<div class="container-xl">
  <!-- Page title -->
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title">
          站点
        </h2>
      </div>
      <!-- Page title actions -->
      <div class="col-auto ms-auto d-print-none">
        <div class="btn-list">
          <a href="javascript:show_site_modal()" class="btn btn-primary d-none d-sm-inline-block">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
            新增站点
          </a>
          <a href="javascript:show_site_modal()" class="btn btn-primary d-sm-none btn-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
          </a>
        </div>
      </div>
    </div>
  </div>
</div>
{% if Sites %}
<div class="page-body">
  <div class="container-xl">
    <div class="row row-cards">
      {% for Site in Sites %}
      <div class="col-12 col-md-6 col-xl-4">
        <div class="card card-link-pop">
          <div class="card-header">
            <h3 class="card-title">
              <strong>{{ Site.name }}</strong>
            </h3>
            <div class="card-actions btn-actions">
              <a href="javascript:edit_site('{{ Site.id }}')" class="btn-action" title="编辑站点">
                <svg xmlns="http://www.w3.org/2000/svg" class="icon ms-1" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M9 7h-3a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-3" /><path d="M9 15h3l8.5 -8.5a1.5 1.5 0 0 0 -3 -3l-8.5 8.5v3" /><line x1="16" y1="5" x2="19" y2="8" /></svg>
              </a>
              <a href="javascript:remove_site('{{ Site.name }}', '{{ Site.id }}')" class="btn-action" title="删除站点">
                <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><desc>Download more icon variants from https://tabler-icons.io/i/x</desc><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="18" y1="6" x2="6" y2="18" /><line x1="6" y1="6" x2="18" y2="18" /></svg>
              </a>
            </div>
          </div>
          <div class="card-body">
            <dl class="row">
              {% if Site.signurl %}
              <dt class="col-3">签到地址:</dt>
              <dd class="col-9">{{ Site.signurl }}</dd>
              {% endif %}
              {% if Site.cookie %}
              <dt class="col-3">COOKIE:</dt>
              <dd class="col-9">***</dd>
              {% endif %}
              {% if Site.rssurl %}
              <dt class="col-3">RSS地址:</dt>
              <dd class="col-9">***</dd>
              {% endif %}
              {% if Site.rule %}
              <dt class="col-3">过滤规则:</dt>
              <dd class="col-9"><span class="badge badge-outline text-orange me-2">{{ Site.rule_name }}</span></dd>
              {% endif %}
              {% if Site.free %}
              <dt class="col-3">促销:</dt>
              <dd class="col-9"><span class="badge me-2 bg-green">{{ Site.free }}</span></dd>
              {% endif %}
            </dl>
          </div>
        </div>
      </div>
      {% endfor %}
    </div>
  </div>
</div>
{% else %}
<div class="page-body">
  <div class="container-xl d-flex flex-column justify-content-center">
    <div class="empty">
      <div class="empty-img"><img src="./static/img/sign_in.svg" height="128"  alt="">
      </div>
      <p class="empty-title">没有站点</p>
      <p class="empty-subtitle text-muted">
        没有添加任何站点，请点击”新增站点“按钮。
      </p>
    </div>
  </div>
</div>
{% endif %}
<div class="modal modal-blur fade" id="modal-site" tabindex="-1" role="dialog" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="card border-top-0">
        <div class="card-header">
          <h5 class="modal-title" id="modal-site-title">新增站点</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-lg-8">
              <div class="mb-3">
                <label class="form-label required">名称</label>
                <input type="hidden" id="site_id" />
                <input type="text" class="form-control" id="site_name" placeholder="自定义站点名称">
              </div>
            </div>
            <div class="col-lg-4">
              <div class="mb-3">
                <label class="form-label">优先级 <span class="form-help bg-red-lt" title="多个站点同时命中订阅时选择下载的优先级，数值越小优先级越高" data-bs-toggle="tooltip">?</span></label>
                <select class="form-select" id="site_pri">
                  <option value="1" selected>1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  <option value="5">5</option>
                  <option value="6">6</option>
                  <option value="7">7</option>
                  <option value="8">8</option>
                  <option value="9">9</option>
                  <option value="10">10</option>
                  <option value="11">11</option>
                  <option value="12">12</option>
                  <option value="13">13</option>
                  <option value="14">14</option>
                  <option value="15">15</option>
                  <option value="16">16</option>
                  <option value="17">17</option>
                  <option value="18">18</option>
                  <option value="19">19</option>
                  <option value="20">20</option>
                </select>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-lg-12">
              <div class="mb-3">
                <label class="form-label">签到地址 <span class="form-help bg-red-lt" title="用于站点自动签到，以及上传下载流量统计等，一般为mybonus.php或attendance.php，即PT站点击签到按钮跳转的页面；部分站没有独立的签到页面时可以配置为首页，实现定时登录保号" data-bs-toggle="tooltip">?</span></label>
                <input type="text" class="form-control" id="site_signurl" value="" placeholder="站点签到页面URL" autocomplete="off">
              </div>
            </div>
          </div>
          <div class="col-lg-12">
            <div class="mb-3">
              <label class="form-label">COOKIE <span class="form-help bg-red-lt" title="站点的Cookie，用于站点自动签到，以及上传下载流量统计等，需要在浏览器中F12->网络页抓取" data-bs-toggle="tooltip">?</span></label>
              <textarea class="form-control" id="site_cookie" rows="2" placeholder="站点COOKIE"></textarea>
            </div>
          </div>
          <div class="row">
            <div class="col-lg-12">
              <div class="mb-3">
                <label class="form-label">订阅地址 <span class="form-help bg-red-lt" title="站点订阅源URL地址，用于电影、电视剧订阅，点击站点RSS图标获取" data-bs-toggle="tooltip">?</span></label>
                <input type="text" class="form-control" id="site_rssurl" value="" placeholder="站点RSS订阅URL" autocomplete="off">
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-lg-6">
              <div class="mb-3">
                <label class="form-label">过滤规则 <span class="form-help bg-red-lt" title="选择该站点使用的规则组，在设置->过滤规则中设置规则，选择了规则组后该站点只有符合规则的种子才会被订阅" data-bs-toggle="tooltip">?</span></label>
                <select class="form-select" id="site_rule">
                  <option value="" selected>请选择</option>
                  {% for RuleGroup in RuleGroups %}
                  <option value="{{ RuleGroup.id }}">{{ RuleGroup.name }}</option>
                  {% endfor %}
                </select>
              </div>
            </div>
            <div class="col-lg-6">
              <div class="mb-3">
                <label class="form-label">促销 <span class="form-help bg-red-lt" title="选择免费时，只有促销免费的种子才会被订阅命中和下载，站点正在不断适配中，如下拉框中没有免费选项则代表暂不支持该站点" data-bs-toggle="tooltip">?</span></label>
                <select class="form-select" id="site_free">
                  <option value="" selected>全部</option>
                  <option value="FREE">免费</option>
                  <option value="2XFREE">2X免费</option>
                </select>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button id="site_close_btn" class="btn btn-link">
          关闭
        </button>
        <button id="add_or_edit_site_btn" class="btn btn-primary ms-auto">
          新增
        </button>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-rss-rule" tabindex="-1" role="dialog" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="card border-top-0">
        <div class="card-header">
          <h5 class="modal-title" id="modal-rss-rule-title">全局规则</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-lg-12">
              <div class="mb-3">
                <label class="form-label">优先规则 <span class="form-help bg-red-lt" title="【优先选择的关键字】：种子名称或副标题中如果有这些关键字则会被优先选择，优先级高于站点&#10;1、对大小写不敏感，不能带引号，配置为空为不启用该规则&#10;2、可以配置多行，一行是一个关键字或正则表达式，排前面的优先级高" data-bs-toggle="tooltip">?</span></label>
                <pre id="rss_note_pre"></pre>
                <textarea class="form-control" id="rss_note" rows="2" placeholder="优先选择的关键字或正则表达式"></textarea>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button id="rule_close_btn" class="btn btn-link" data-bs-dismiss="modal">
          关闭
        </button>
        <button id="save_or_edit_rule_btn" class="btn btn-primary ms-auto">
          编辑/保存
        </button>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  //打开新增站点框
  function show_site_modal(){
    $("#site_id").val('');
    $("#site_name").val('');
    $("#site_pri").val('1');
    $("#site_rssurl").val('');
    $("#site_signurl").val('');
    $("#site_cookie").val('');
    $("#site_rule").val('');
    $("#site_close_btn").text("关闭");
    $("#modal-site-title").text("新增站点");
    $("#add_or_edit_site_btn").text("新增");
    $("#site_free").empty();
    $("#site_free").append('<option value="" selected>全部</option>');
    $("#modal-site").modal("show");
  }
  //编辑站点
  function edit_site(tid){
    $("#site_id").val(tid);
    $("#site_close_btn").text("删除");
    //根据ID查询详细信息
    ajax_post("get_site", {"id": tid}, function(ret){
      if(ret.site){
        $("#site_id").val(ret.site.id);
        $("#site_name").val(ret.site.name);
        $("#site_pri").val(ret.site.pri);
        $("#site_rssurl").val(ret.site.rssurl);
        $("#site_signurl").val(ret.site.signurl);
        $("#site_cookie").val(ret.site.cookie);
        $("#modal-site-title").text("编辑站点");
        $("#add_or_edit_site_btn").text("修改");
        $("#modal-site").modal("show");
      }
      $("#site_free").empty();
      $("#site_free").append('<option value="" selected>全部</option>');
      if(ret.site_free){
        $("#site_free").append('<option value="FREE">免费</option>');
      }
      if(ret.site_2xfree){
        $("#site_free").append('<option value="2XFREE">2X免费</option>');
      }
      $("#site_free").val(ret.site.free);
      $("#site_rule").val(ret.site.rule);
    });
  }

  //删除站点
  function del_site(tid){
    ajax_post("del_site", {"id":tid}, function(ret){
      navmenu('site');
    });
  }

  //新增站点
  function add_or_edit_site(){
    //id
    var site_id = $("#site_id").val();
    //名称
    var site_name = $("#site_name").val();
    if(site_name == ""){
      $("#site_name").addClass("is-invalid");
      return;
    }else{
      $("#site_name").removeClass("is-invalid");
    }
    //优先级
    var site_pri = $("#site_pri").val();
    if(site_pri){
      $("#site_pri").removeClass("is-invalid");
    }else{
      $("#site_pri").addClass("is-invalid");
      return;
    }
    //签到URL
    var site_signurl = $("#site_signurl").val();
    if(site_signurl){
      if(site_signurl.startsWith('http')){
        $("#site_signurl").removeClass("is-invalid");
      }else{
        $("#site_signurl").addClass("is-invalid");
        return;
      }
    }
    //Cookie
    var site_cookie = $("#site_cookie").val();
    //RSS URL
    var site_rssurl = $("#site_rssurl").val();
    if(site_rssurl){
      if(site_rssurl.startsWith('http')){
        $("#site_rssurl").removeClass("is-invalid");
      }else{
        $("#site_rssurl").addClass("is-invalid");
        return;
      }
    }
    var site_rule = $("#site_rule").val();
    var site_free = $("#site_free").val();
    var data = {"site_id":site_id,
          "site_name":site_name,
          "site_pri":site_pri,
          "site_rssurl":site_rssurl,
          "site_signurl":site_signurl,
          "site_cookie":site_cookie,
          "site_note":site_free + "|" + site_rule}

    $("#add_or_edit_site_btn").attr("disabled", true);
    $("#add_or_edit_site_btn").text("处理中...");
    // 发送请求
    ajax_post("update_site", data, function(ret){
      $("#add_or_edit_site_btn").attr("disabled", false);
      $("#modal-site").modal("hide");
      $("#add_or_edit_site_btn").text("新增");
      //刷新页面
      navmenu('site');
    });
  }

  //关闭或者删除
  function close_or_del(){
    $("#modal-site").modal("hide");
    tid = $("#site_id").val();
    if(tid){
      del_site(tid);
    }
  }

  //绑定事件
  $("#add_or_edit_site_btn").click(function(){
      add_or_edit_site();
  });
  $("#site_close_btn").click(function(){
      close_or_del();
  });

  //删除站点
  function remove_site(name, siteid){
    show_ask_modal("是否确认删除站点 " + name + "？", function(){
      hide_ask_modal();
      del_site(siteid);
    });
  }

</script>